/********************************************************************
 * PROJECT: CSAIR ROST PROJECT.
 * SUPPLIER: FOREVER OPEN SOURCE SOFTWARE INC.
 ********************************************************************
 * FILE: GanttGrid.java
 * (C) Copyright Foliage Team 2013, All Rights Reserved.
 ********************************************************************/
package org.foliage.rcp.ui.custom;

import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DragSourceListener;
import org.eclipse.swt.dnd.DropTargetListener;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Sash;
import org.foliage.rcp.ui.events.IGanttListener;
import org.foliage.rcp.ui.handlers.GanttGridJointHandler;
import org.foliage.rcp.ui.handlers.IGanttGridJointHandler;
import org.foliage.rcp.ui.internal.proxy.IScrollBarProxy;
import org.foliage.rcp.ui.preference.DefaultGanttColorManager;
import org.foliage.rcp.ui.preference.DefaultGanttPaintManager;
import org.foliage.rcp.ui.preference.DefaultGanttSettings;
import org.foliage.rcp.ui.preference.IGanttColorManager;
import org.foliage.rcp.ui.preference.IGanttPaintManager;
import org.foliage.rcp.ui.preference.IGanttSettings;
import org.foliage.rcp.ui.widgets.Gantt;
import org.foliage.rcp.ui.widgets.GanttTask;
import org.foliage.rcp.ui.widgets.Grid;
import org.foliage.rcp.ui.widgets.GridItem;

public class GanttGrid extends Composite {

	// ----------------------------------------------- 

	/** 甘特图控件全局参数。 */
	private int usedGanttStyle = 0;
	/** 报表控件全局参数。 */
	private int usedGridStyle = 0;

	// ----------------------------------------------- 

	/** 甘特图面板配置参数。 */
	private IGanttSettings settings = null;
	/** 甘特图绘画适配器。 */
	private IGanttPaintManager paintManager = null;
	/** 甘特图颜色适配器。 */
	private IGanttColorManager colorManager = null;
	/** 联合驱动接口。 */
	private IGanttGridJointHandler jointHandler = null;

	// ----------------------------------------------- 

	/** 表格画布控件。 */
	private Grid grid = null;
	/** 甘特图画布控件。 */
	private Gantt gantt = null;
	/** 尺寸拖拽滑块控件。 */
	private Sash sash = null;

	// ----------------------------------------------- 

	public GanttGrid(Composite parent, int style) {
		this(parent, style, null, null, null);
	}

	public GanttGrid(Composite parent, int style, IGanttSettings settings, IGanttPaintManager paintManager, IGanttColorManager colorManager) {
		super(parent, SWT.NONE);
		usedGanttStyle = checkGanttStyle(style);
		usedGridStyle = checkGridStyle(style);
		this.settings = settings != null ? settings : new DefaultGanttSettings();
		this.colorManager = colorManager != null ? colorManager : new DefaultGanttColorManager();
		this.paintManager = paintManager != null ? paintManager : new DefaultGanttPaintManager();
		this.jointHandler = new GanttGridJointHandler(new GanttGrid[] { this });
		init();
	}

	// ----------------------------------------------- 

	private static int checkGanttStyle(int style) {
		int mask = SWT.BORDER | SWT.MULTI | SWT.SINGLE | SWT.NO_FOCUS | SWT.V_SCROLL | SWT.H_SCROLL;
		int newStyle = style & mask;
		return newStyle;
	}

	private static int checkGridStyle(int style) {
		int mask = SWT.BORDER | SWT.MULTI | SWT.SINGLE | SWT.NO_FOCUS | SWT.H_SCROLL;
		int newStyle = style & mask;
		return newStyle;
	}

	// ----------------------------------------------- 

	private void init() {
		setLayout(new FormLayout());
		{
			sash = new Sash(this, SWT.VERTICAL);
			FormData data = new FormData();
			data.left = new FormAttachment(0, 1, 250);
			data.top = new FormAttachment(0, 1, 0);
			data.bottom = new FormAttachment(1, 1, 0);
			sash.setLayoutData(data);
			sash.addListener(SWT.Selection, new Listener() {

				@Override
				public void handleEvent(Event event) {
					if (event.doit && event.detail != SWT.DRAG) {
						jointHandler.jointSashMovement(event);
					}
				}

			});
		}
		{
			grid = new Grid(this, usedGridStyle);
			grid.setItemHeight(settings.getTaskHeight() + settings.getTaskSpacer() - 1);
			FormData data = new FormData();
			data.left = new FormAttachment(0, 1, 0);
			data.right = new FormAttachment(sash, 0, SWT.LEFT);
			data.top = new FormAttachment(0, 1, 0);
			data.bottom = new FormAttachment(1, 1, 0);
			grid.setLayoutData(data);
		}
		{
			gantt = new Gantt(this, usedGanttStyle, settings, colorManager, paintManager);
			FormData data = new FormData();
			data.left = new FormAttachment(sash, 0, SWT.RIGHT);
			data.right = new FormAttachment(1, 1, 0);
			data.top = new FormAttachment(0, 1, 0);
			data.bottom = new FormAttachment(1, 1, 0);
			gantt.setLayoutData(data);
			gantt.setJointHandler(jointHandler);
		}
		// TODO(deathknight0718@gmail.com): 左右面板垂直联动。
		grid.setVerticalScrollBarProxy(gantt.getVerticalScrollBarProxy());
	}

	// ----------------------------------------------- 

	public void removeAll() {
		grid.removeAll();
		gantt.removeAll();
	}

	// ----------------------------------------------- 

	public Grid getGrid() {
		return grid;
	}

	public Gantt getGantt() {
		return gantt;
	}

	public Sash getSash() {
		return sash;
	}

	// ----------------------------------------------- 

	public void setTransfer(Transfer[] transfer) {
		gantt.setTransfer(transfer);
	}

	public void addDragListener(DragSourceListener listener) {
		gantt.addDragListener(listener);
	}

	public void addDropListener(DropTargetListener listener) {
		gantt.addDropListener(listener);
	}

	public void addGanttListener(IGanttListener listener) {
		gantt.addGanttListener(listener);
	}

	public void removeGanttListener(IGanttListener listener) {
		gantt.removeGanttListener(listener);
	}

	// ----------------------------------------------- 

	public boolean isHeaderVisible() {
		return grid.getHeaderVisible() && gantt.isHeaderVisible();
	}

	public void setHeaderVisible(boolean headerVisible) {
		grid.setHeaderVisible(headerVisible);
		gantt.setHeaderVisible(headerVisible);
	}

	public IGanttGridJointHandler getJointHandler() {
		return jointHandler;
	}

	public void setJointHandler(IGanttGridJointHandler jointHandler) {
		this.jointHandler = jointHandler;
		this.gantt.setJointHandler(jointHandler);
	}

	public IScrollBarProxy getGridHorizontailScrollBarProxy() {
		return grid.getHorizontalScrollBarProxy();
	}

	public void setGridHorizontailScrollBarProxy(IScrollBarProxy hScroll) {
		grid.setHorizontalScrollBarProxy(hScroll);
	}

	// ----------------------------------------------- 

	public GanttGridColumn getColumn(int index) {
		return (GanttGridColumn) grid.getColumn(index).getData(GanttGridColumn.DATA_KEY);
	}

	public GanttGridItem[] getItems() {
		GridItem[] gItems = grid.getItems();
		GanttGridItem[] items = new GanttGridItem[gItems.length];
		for (int i = 0; i < gItems.length; i++) {
			items[i] = (GanttGridItem) gItems[i].getData(GanttGridItem.DATA_KEY);
		}
		return items;
	}

	public GanttGridTask[] getTasks() {
		GanttTask[] gTasks = gantt.getTasks();
		GanttGridTask[] tasks = new GanttGridTask[gTasks.length];
		for (int i = 0; i < gTasks.length; i++) {
			tasks[i] = (GanttGridTask) gTasks[i].getData(GanttGridItem.DATA_KEY);
		}
		return tasks;
	}

}
